
#ifndef _MEMBER_BSOURCE_H_
#define _MEMBER_BSOURCE_H_

#include <BMap>
#include <BClass>
#include <BScript>
#include <BSource>
#include "IVariantStack.h"
#include "member_BClass.h"

#define source_member(source)	((member_BSource*)((BSource*)source)->_ptr)

namespace BWE
{
	class Function : public BThing
	{
	public:
		inline Function()
		{
			line = -1;
			cfunc = 0;
		}

		inline bool empty() const
		{
			return cfunc == 0 && sfunc == 0;
		}

		int					line;
		BString				type;
		CFunc				cfunc;
		BExpHolder			sfunc;
		BArray<BVariant>	params;
	};
	typedef BArray<Function*>		FunctionArray;
	typedef BHolder<Function>		FunctionHolder;
	typedef BArray<FunctionHolder>	FunctionHolderArray;

	class member_BSource
	{
	public:
		member_BSource(BSource* source);
		~member_BSource();

		BString& errorMessage(BExp* exp = 0);

		bool compile();

		BExp* create(Syntax syntax);
		BExp* compile(const BString& text, int& pos, int& line);
		BExp* analyse(BExpHolderArray& exps);
		BExp* createValue(BString& word);
		BExp* createMacro(const BString& text, int& pos, int& line);
		BExp* createLineNote(const BString& text, int& pos, int& line);
		BExp* createBlockNote(const BString& text, int& pos, int& line);
		bool collateParams(BExpHolderArray& exps);

		bool checkExp(BExp* exp);
		bool checkType(const BString& text);
		bool checkOpera(const BString& word);
		bool checkName(const BString& name);
		Syntax checkSyntax(const BString& text);
		BString checkFormula(BExpHolderArray& exps);
		BClass* checkSelf();
		BVariant* checkVariant(const BString& name);
		BVariant* checkVariant(const BExpHolderArray& exps);
		bool checkPath(BExpHolderArray& exps);

		int nextWord(BString& word, const BString& text, int& pos, int& line);
		int findSplit(BExpHolderArray& exps);
		BString funcName(BExp* exp);
		BClass* findClass(const BString& type);
		Function* findFunction(const BString& name);
		bool matchFunction(BExp* exp, Function* function);
		bool matchMethod(BExp* exp, Method* method);

		BSource*			boss;
		BScript*			script;
		bool				dirty;
		BString				text;

		bool					error;
		BValue					message;
		IVariantStack			stack;
		BStringArray			srcnames;
		BExpHolderArray			document;
		BArray<BExpHolder>		includes;
		BArray<BExpHolder>		declares;
		BMap<BString, BClassHolder>	classMap;
		BMap<BString, FunctionHolder>	functionMap;

	};
}

#endif